package com.gc.ordersys2.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gc.ordersys2.common.result.Result;
import com.gc.ordersys2.common.result.ResultUtils;
import com.gc.ordersys2.entity.Type;
import com.gc.ordersys2.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.lang.model.element.TypeElement;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cuijianhao
 * @since 2024-09-05
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    ITypeService service;

    @RequestMapping("/getList")
    public Result<Type> getList(Integer page,Integer limit){
        //封装查询条件，排序方式
        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("tname","爆品");
        queryWrapper.orderByAsc("id");

        //分页
        IPage<Type> p = new Page<>(page,limit);


        //查询---带条件分页查询
        IPage list = service.page(p, queryWrapper);
        List records = list.getRecords();   //当前页的数据
        int total = (int)list.getTotal();   //总条数

        return ResultUtils.returnSuccessLayui(records,total);
    }
    @RequestMapping("/del")
    public Object del(Integer id){
        if(service.removeById(id)){
            return ResultUtils.returnSuccess();
        }else{
            return ResultUtils.returnFail();
        }
    }
    @RequestMapping("/add")
    public Object add(Type type){
        if(service.save(type)){
            return ResultUtils.returnSuccess();
        }else{
            return ResultUtils.returnFail("添加失败");
        }
    }
    @RequestMapping("/update")
    public Object update(Type type){
        if(service.updateById(type)){
            return ResultUtils.returnSuccess();
        }else{
            return ResultUtils.returnFail("修改失败");
        }
    }
    @RequestMapping("/getAllList")
    public Result<Type>getAllList(){
        return ResultUtils.returnDataSuccess(service.list());
    }
}
